package com.liuqi.aiagent.service.impl;

import com.liuqi.aiagent.mapper.AgentToolBindingMapper;
import com.liuqi.aiagent.mapper.WebhookToolMapper;
import com.liuqi.aiagent.service.AgentEventService;
import com.liuqi.aiagent.service.WebhookToolService;
import com.liuqi.aiagent.transfer.ApiResult;
import com.liuqi.aiagent.transfer.dto.WebhookToolSaveDto;
import com.liuqi.aiagent.transfer.entity.WebhookTool;
import com.liuqi.aiagent.transfer.mapstruct.WebhookToolMapStruct;
import com.liuqi.aiagent.transfer.model.Result;
import com.liuqi.aiagent.transfer.model.ResultList;
import com.liuqi.aiagent.transfer.vo.WebhookToolBriefVo;
import com.liuqi.aiagent.transfer.vo.WebhookToolVo;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liuqi
 * @date 2025/7/15
 **/
@Slf4j
@Service
@AllArgsConstructor
public class WebhookToolServiceImpl implements WebhookToolService {

    private final WebhookToolMapper webhookToolMapper;

    private final AgentEventService agentEventService;

    private final AgentToolBindingMapper agentToolBindingMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result imports(List<WebhookToolSaveDto> webhookTools) {
        WebhookToolMapStruct mapStruct = WebhookToolMapStruct.INSTANCE;
        // 触发编辑的工具ID
        List<Integer> editToolIds = new ArrayList<>();
        for (WebhookToolSaveDto dto : webhookTools) {
            Integer id = webhookToolMapper.findIdByGroupNameAndToolName(dto.getGroupName(), dto.getToolName());
            if (id != null) {
                editToolIds.add(id);

                log.info("工具[{}]已存在, 执行更新操作", dto.getToolName());
                WebhookTool webhookTool = mapStruct.from(dto);
                webhookTool.setId(id);
                webhookToolMapper.update(webhookTool);
            } else {
                // save
                webhookToolMapper.insert(mapStruct.from(dto));
            }
        }

        // 发布工具变更事件
        if (!editToolIds.isEmpty()) {
            agentEventService.webhookToolEditEvent(editToolIds, "webhook");
        }

        return ApiResult.success();
    }

    @Override
    public ResultList<WebhookToolVo> findAll() {
        List<WebhookTool> webhookTools = webhookToolMapper.findAll();
        List<WebhookToolVo> webhookToolVos = WebhookToolMapStruct.INSTANCE.toVos(webhookTools);
        return ApiResult.success(webhookToolVos, webhookToolVos.size());
    }

    @Override
    public ResultList<WebhookToolBriefVo> findAllOfBrief() {
        List<WebhookTool> webhookTools = webhookToolMapper.findAllOfBrief();
        List<WebhookToolBriefVo> webhookToolBriefVos = WebhookToolMapStruct.INSTANCE.toBriefVos(webhookTools);
        return ApiResult.success(webhookToolBriefVos, webhookToolBriefVos.size());
    }

    @Override
    public Result deleteById(int id) {
        List<Integer> agentIds = agentToolBindingMapper.findAgentIdByWebhookToolId(id);
        if (agentIds != null && !agentIds.isEmpty()) {
            return ApiResult.fail("工具已被Agent关联, 先解除关联后删除");
        }
        webhookToolMapper.deleteById(id);
        return ApiResult.success();
    }

}
